<?php 
// this file will be used to display the inventory list
// connect to mysql database
require("../storescripts/config.php");
//start session
session_start();

if (!isset($_SESSION["manager"])) {
    header("location: admin_login.php"); 
    exit();
}
// check if the manager session is in the database
$managerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); // filter everything but numbers
$manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]); // filter everything but numbers and letters
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]); // filter everything but numbers and letters

$sql = mysql_query("SELECT * FROM admin WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1"); // query the person
// check if the person exists in the database
$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount == 0) { // evaluate the count
	 echo "Your login session data is not on record in the database.";
     exit();
}
?>
<?php 
// deleting an item
if (isset($_GET['deleteid'])) {?>
    <script language="javascript">
	<!--prompt for the admin to confirm deletion -->
	if(window.confirm("Are you sure you want to delete that record?")) {
		document.location = "inventory_list.php?yesdelete=<?php echo $_GET['deleteid']; ?>";
	} else {
		document.location="inventory_list.php";	
	}
	</script>
<?php
	exit();
}
if (isset($_GET['yesdelete'])) {
	// remove item from system and delete its picture
	// delete from database
	$id_to_delete = $_GET['yesdelete'];
	$sql = mysql_query("DELETE FROM products WHERE id='$id_to_delete' LIMIT 1") or die (mysql_error());
	// unlink the image from server
	// remove the picture
    $pictodelete = ("../inventory_images/$id_to_delete.jpg");
    if (file_exists($pictodelete)) {
       		unlink($pictodelete);
    }
	header("location: inventory_list.php"); 
    exit();
}
?>
<?php 
// grabs the whole list for viewing
$product_list = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC");//query the products
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
	while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
			 $product_name = $row["product_name"];
			 $price = $row["price"];
			 $category = $row["category"];
			 $subcategory = $row["subcategory"];
			 $subcategory1 = $row["subcategory1"];
			 $is_featured = $row["is_featured"];
			 $quantity = $row["original_quantity"];
			 $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
			if($is_featured == 1) {
				$is_featured = "Yes";
			} else {
				$is_featured = "No";
			}
			 $product_list .= "
			<tr align='center'>
			<td>$date_added</td>
			<td>$id</td>
			<td>$product_name</td>
			<td>$$price</td>
			<td>$category</td>
			<td>$subcategory</td>
			<td>$subcategory1</td>";
			if($quantity == 0) {
				$product_list .= "<td>Out of Stock</td>";
			} else if($quantity <= 2) {
				$product_list .= "<td>Low Stock</td>";
			} 
			else {
				$product_list .= "<td>$quantity</td>";
			}
			$product_list .= "<td>$is_featured</td>
			<td><a href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a></td>
			</tr>";
    }
} else {
	$product_list = "You have no products listed in your store yet";
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Inventory List</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../css/style.css" type="text/css" media="screen" />
</head>

<body>
<!-- Main Shell -->
<div class="shell">
	
  <!-- Header -->
  <div id="header"><h1 id="logo"><a href="index.php">Luvo Fashion</a></h1>
	<div align="right" style="padding-right:5px; padding-top:40px;">
		<a style="color:#ccc;" href="index.php">Back</a>&nbsp; |
		<a style="color:#ccc;" href="logout.php">Logout</a>
	</div>
  </div>
  <!-- End Header -->

  <!-- Main -->
  <div id="main">
    <br/>
  	<div align="right"><a href="inventory_form.php">+ Add New Inventory Item</a></div>
    <br />
	<h1>Inventory List</h1>
    <div align="center" style=" padding-top:10px; line-height:2em;">
        <br />
		<table width="80%" border="1" cellspacing="0" cellpadding="1">
		<tr align="center">
		<td class="cartTable">Date Added</td>
		<td class="cartTable">Product ID</td>
		<td class="cartTable">Product Name</td>
		<td class="cartTable">Price</td>
		<td class="cartTable">Category</td>
		<td class="cartTable">Subcategory</td>
		<td class="cartTable">Subcategory1</td>
		<td class="cartTable">Quantity</td>
		<td class="cartTable">Featured Item</td>
		<td class="cartTable">Actions</td>
		</tr>
        <?php echo $product_list; ?>
		</table>
	</div>
    <br  />
    <br />
    
  </div>
<!-- End Main -->
  
<!-- Footer -->
   <div style="padding-top:5px;" id="footer">
    <p class="left">Manage Inventory</p>
    <p class="right"> &copy; 2010 Luvo Fashion.</p>
    </div>
<!-- End Footer -->

</div>
<!-- End Shell -->

</body>
</html>
